package com.wfcm.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wfcm.dao.WfMemberSessionDao;
import com.wfcm.entity.WfMemberSessionEntity;
import com.wfcm.service.WfMemberSessionService;



@Service("wfMemberSessionService")
public class WfMemberSessionServiceImpl implements WfMemberSessionService {
	@Autowired
	private WfMemberSessionDao wfMemberSessionDao;
	
	@Override
	public WfMemberSessionEntity queryObject(long userid){
		return wfMemberSessionDao.queryObject(userid);
	}
	
	@Override
	public List<WfMemberSessionEntity> queryList(Map<String, Object> map){
		return wfMemberSessionDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return wfMemberSessionDao.queryTotal(map);
	}
	
	@Override
	public void save(WfMemberSessionEntity wfMemberSession){
		wfMemberSessionDao.save(wfMemberSession);
	}
	
	@Override
	public void update(WfMemberSessionEntity wfMemberSession){
		wfMemberSessionDao.update(wfMemberSession);
	}
	
	@Override
	public void delete(Integer userid){
		wfMemberSessionDao.delete(userid);
	}
	
	@Override
	public void deleteBatch(Integer[] userids){
		wfMemberSessionDao.deleteBatch(userids);
	}

	@Override
	public WfMemberSessionEntity queryByToken(String token) {
		// TODO Auto-generated method stub
		return wfMemberSessionDao.queryByToken(token);
	}

	
	@Override
	public Map<String, Object> createToken(long userId) {
		//生成一个token
		String token = UUID.randomUUID().toString();
		//当前时间
		Long now = new Date().getTime();

		//过期时间
		//Date expireTime = new Date(now.getTime() + EXPIRE * 1000);

		//判断是否生成过token
		WfMemberSessionEntity memberSessionEntity = queryObject(userId);
		if(memberSessionEntity == null){
			memberSessionEntity = new WfMemberSessionEntity();
			memberSessionEntity.setUserid(userId);
			memberSessionEntity.setSession(token);
			
			memberSessionEntity.setTime(now.intValue());
			
			//tokenEntity.setExpireTime(expireTime);

			//保存token
			save(memberSessionEntity);
		}else{
			memberSessionEntity.setSession(token);
			memberSessionEntity.setTime(now.intValue());
			//tokenEntity.setExpireTime(expireTime);

			//更新token
			update(memberSessionEntity);
		}

		Map<String, Object> map = new HashMap<>();
		map.put("token", token);
		//map.put("expire", EXPIRE);

		return map;
	}

	
}
